package io.renren.modules.app.message.handler;

import cn.hutool.core.thread.ThreadUtil;
import io.renren.modules.app.message.MessageResult;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * @author :
 * @version V1.0
 * @Project:
 * @Package
 * @Description: 消息轮询器
 * @date Date :
 **/
@Slf4j
public class MessageLoopHandler implements Runnable{

//    private  JdVopApiHandler jdVopApiHandler;

    private  JdVopMessageHandler jdVopMessageHandler;

    private String       types;

//    public MessageLoopHandler(JdVopApiHandler jdVopApiHandler, JdVopMessageHandler jdVopMessageHandler,String types) {
    public MessageLoopHandler(JdVopMessageHandler jdVopMessageHandler,String types) {
//        this.jdVopApiHandler = jdVopApiHandler;
        this.jdVopMessageHandler = jdVopMessageHandler;
        this.types=types;
    }

    @Override
    public void run() {
        this.jdVopLoop(this.types);
    }
    /**
     * 京东消息轮询
     * 1.等前n条数据处理完毕后才轮询后面的消息
     * 2.消息如何被程序已读则消息剩余个数-1(无论是否被删除)
     * 3.
     */
    public void jdVopLoop(String types){
        while (true){
            int total=this.jdVopMessageHandler.getMsgTotal();
            log.info("当前消息剩余个数：{}",total);
            if(total <= 0 ){
//                log.info("轮询京东消息-------start---,消息总量：0");
//                List<MessageResult> result= this.jdVopApiHandler.batchGetMessages(types);
//                int size=result.size();
//                this.jdVopMessageHandler.setMsgTotal(size);
//                this.jdVopMessageHandler.batchRead(result);  //异步批量处理
//                log.info("轮询京东消息-------end---,消息总量：{}",size);
                List<MessageResult> result = new ArrayList<>();
                MessageResult messageResult = new MessageResult();
                result.add(messageResult);
                this.jdVopMessageHandler.batchRead(result);
            }
            ThreadUtil.sleep(15, TimeUnit.SECONDS);//等待5s后轮询
        }
    }

}
